package net.ifilm.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author wangjunneil@gmail.com
 */
public class ThreadPool {

    private static final Log log = LogFactory.getLog(ThreadPool.class);

    private TaskRunnable[] taskRunnables;

    public ThreadPool(int poolSize) throws SpiderException {
        taskRunnables = new TaskRunnable[poolSize];
    }

    public void start() {
        String threadName = null;
        for (int i = 0; i < taskRunnables.length; i++) {
            threadName =  "Spider-" + i;
            if (log.isDebugEnabled()) {
                log.debug(threadName + " thread already is running ...");
            }

            taskRunnables[i] = new TaskRunnable(Constants.messageQueue, threadName);
            taskRunnables[i].start();
        }
    }
    public void stop() {
        for (int i = 0; i < taskRunnables.length; i++) {
            taskRunnables[i].stopRunning();
        }
    }
}
